package Data::Timeline::IScrobbler;

use strict;
use warnings;
use DateTime::Format::DateParse;



our $VERSION = '0.01';


use base qw(Data::Timeline::Builder);


__PACKAGE__->mk_scalar_accessors(qw(log_filename));


use constant DEFAULTS => (
    log_filename => "$ENV{HOME}/Library/Logs/iScrobbler.log",
);


sub create {
    my $self = shift;

    my $timeline = $self->make_timeline;

    my $filename = $self->log_filename;
    open my $fh, '<', $filename or die "can't open $filename: $!\n";

    while (<$fh>) {
        next unless /^\[(.*?)\]-\[VERB\] Added '(.*)'/;
        $timeline->entries_push($self->make_entry(
            timestamp   => DateTime::Format::DateParse->parse_datetime($1),
            description => $2,
            type        => 'iscrobbler',
        ));
    }

    close $fh or die "can't close $filename: $!\n";

    $timeline;
}


1;


__END__

{% USE p = PodGenerated %}

=head1 NAME

{% p.package %} - Build a timeline from tracks recorded by iScrobbler

=head1 SYNOPSIS

    my $timeline = {% p.package %}->new->create;

=head1 DESCRIPTION

This class parses the logs created by the Mac OS X application iScrobbler and
creates a timeline of the recently played tracks. iScrobbler is an
application for Mac OS X that creates a simple menu extra that submits your
"currently playing" info from iTunes to AudioScrobbler. The advantage over
using the AudioScrobbler web service is that iScrobbler keeps a lot more
recently played tracks history than the web service. See its page at
http://iscrobbler.sourceforge.net/.

The timeline entries generated by this builder have the type C<iscrobbler>.

See the C<eg/hackmusic-text.pl> and C<eg/hackmusic-html.pl> programs in this
distribution for examples. The idea for those programs was inspired by Greg
McCarroll's blog post "Optimal Hacking Music" at
http://drinkbroken.typepad.com/drink_broken/2007/11/set-the-hack--1.html. The
programs show timelines of your recently played iTunes tracks history
alongside your svk commit history and thus give an impression of which music
you like to hack to.

{% p.write_inheritance %}

=head1 METHODS

=over 4

{% p.write_methods %}

=item log_filename

The log filename from which the builder tries to parse the recently played
track information. Defaults to C<~/Library/Logs/iScrobbler.log>.

=item create

Start parsing information from the logfile indicated by the C<log_filename()>.
Returns a L<Data::Timeline> object with the gleaned information.

=back

{% PROCESS standard_pod %}

=cut

